package com.qf.tenant.manager.api;

import com.qf.common.bean.R;
import com.qf.common.dto.DeviceDto;
import com.qf.common.model.QfDevice;
import com.qf.tenant.manager.service.DeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/tenant/device")
@Tag(name = "DeviceController", description = "租户系统设备管理")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/parentId/{parentId}")
    @Operation(summary = "设备接口 - 根据parentId查询",description = "根据parentId查询")
    public R<List> findByParentId(@NotNull @PathVariable(value = "parentId") Long parentId) {
        R<List> result = deviceService.findByParentId(parentId);
        return result;
    }

    @PostMapping("/add")
    @Operation(summary = "设备接口 - 添加设备",description = "租户添加设备")
    public R add(@RequestBody DeviceDto deviceDto){
        R result = deviceService.saveDevice(deviceDto);
        return result;
    }

    @GetMapping("/list")
    @Operation(summary = "设备接口 - 多条件查询",description = "多条件查询设备列表")
    public R list(DeviceDto deviceDto){
        return deviceService.listDevices(deviceDto);
    }

    @GetMapping("/list/{productId}")
    @Operation(summary = "设备接口 - 根据产品ID查询未删除设备列表",description = "根据产品Id查询未删除设备列表")
    public R list(@PathVariable("productId") Long productId){
        return deviceService.listDevices(productId);
    }

    @GetMapping("/listByTenant/{tenantId}")
    @Operation(summary = "设备接口 - 根据租户ID查询所有设备列表",description = "根据租户ID查询所有设备列表")
    public R listByTenant(@PathVariable("tenantId") String tenantId){
        return deviceService.listDevicesByTenant(tenantId);
    }

    @GetMapping("/listByProduct/{productId}")
    @Operation(summary = "设备接口 - 根据产品ID查询所有设备列表",description = "根据产品ID查询所有设备列表")
    public R listByProduct(@PathVariable("productId") String productId){
        return deviceService.listDevicesProduct(productId);
    }

    /**
     * 根据设备ID删除设备，不需要手机验证码
     * @param deviceId
     * @return
     */
    @DeleteMapping("/del/{id}")
    @Operation(summary = "设备接口 - 删除设备",description = "根据设备Id删除")
    public R del(@PathVariable("id") Long deviceId){
        return deviceService.delDevice(deviceId);
    }


    /**
     * 根据设备ID查询设备详情
     * @param deviceId
     * @return
     */
    @GetMapping("/get/{deviceId}")
    @Operation(summary = "设备接口 - 根据设备ID查询设备详情",description = "根据设备ID查询设备详情")
    public R get(@PathVariable("deviceId") String deviceId){
        return deviceService.getDeviceById(deviceId);
    }

    /**
     * 根据条件统计设备数量
     */
    @GetMapping("/count/{tenantId}/{productId}")
    @Operation(summary = "设备接口 - 根据条件统计设备数量",description = "根据条件统计设备数量")
    public R<Map<String, Integer>> count(@PathVariable("tenantId") String tenantId,
                                         @PathVariable("productId") Long productId){
        R<Map<String, Integer>> result = deviceService.selectDeviceCount(tenantId, productId);
        return result;
    }

    /**
     * 判断设备是否在线
     * @param productKey
     * @param deviceKey
     * @return
     */
    @GetMapping("/isOnline/{productKey}/{deviceKey}")
    public R isOnline(@PathVariable(value = "productKey") String productKey,
                    @PathVariable(value = "deviceKey") String deviceKey) {

        return R.ok();
    }


}
